<template>
  <!-- 供应商 -->
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="企业名称">
          <el-input v-model="filters.name" clearable placeholder="请输入企业名称" size="small" />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <el-row :gutter="10" class="button-wrap">
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-search" size="mini" @click="handleQuery()">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-refresh" size="mini" @click="resetQuery()">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" :disabled="single" @click="handleDelete()"><svg-icon icon-class="revoke" :disabled="single" />同意加入</el-button>
      </el-col>
      <el-col :md="12" :lg="14" :xl="17" class="personal-type">
        <el-button icon="el-icon-arrow-left" size="mini" @click="handleReturn()">返回</el-button>
      </el-col>
    </el-row>

    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      style="width: 100%;"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column prop="sysEnterprise.name" :show-overflow-tooltip="true" label="企业名称" min-width="150" align="center" />
      <el-table-column prop="sysEnterprise.sysEnterpriseType" label="企业类型" min-width="120" align="center" :formatter="(row) => formatterPersonSupperType(row)" />
      <el-table-column prop="sysEnterprise.creditCodePic" label="信用代码" :show-overflow-tooltip="true" min-width="150" align="center" />
      <el-table-column prop="sysEnterprise.legalperson" label="联系人" width="150" align="center" />
      <el-table-column prop="sysEnterprise.phone" label="联系电话" width="150" align="center" />
      <el-table-column prop="status" label="状态" width="100" align="center" :formatter="userAddEnterpriseStatusFormat" />
    </el-table>
    <!-- 表格展示end-->

    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { sysApplyRecord } from '@/api/system/enterpriseRecord'
import { listNoPermission } from '@/api/superviseBaseInfo/typeManage/organizationType'
import { getSysDicts } from '@/api/common/getSysDicts'
export default {
  name: 'EnterpriseExamin',
  data() {
    return {
      // 筛选条件
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        pageNum: 1,
        pageSize: 20,
        applyType: '1',
        name: '', // 企业名称
        accountId: this.$store.state.user.sysAccount.id || ''
      },
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [],
      supperTypeList: []
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    this.listSupperTypeType()
    // 获取列表数据
    this.getList()
    // 人员申请加入企业状态
    getSysDicts('sys_person_add_enterprise_status').then(res => {
      this.userAddEnterpriseStatus = res.data.rows
    })
  },
  methods: {
    // 获取企业类型
    async  listSupperTypeType() {
      const { data: { rows }} = await listNoPermission()
      this.supperTypeList = rows
    },
    // 回显企业类型
    formatterPersonSupperType(row) {
      if (row.sysEnterprise && row.sysEnterprise.sysEnterpriseType) {
        const items = this.supperTypeList.filter(item => {
          return item.typeNum === row.sysEnterprise.sysEnterpriseType
        })
        return items.length > 0 ? items[0].typeName : '-'
      }
    },
    // 用户状态字典翻译
    userAddEnterpriseStatusFormat(row, column) {
      return this.selectDictLabel(this.userAddEnterpriseStatus, row.status)
    },
    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await sysApplyRecord(this.filters)
        this.tableData = rows
        this.total = total
        this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格行选中事件 start */
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }

      this.selectRow = []
      this.multipleSelection = val
      console.log(this.multipleSelection)
      this.single = val.length !== 1 || val[0].status !== '2'
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 表格行选中事件 end*/

    /* 按钮 Start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 撤销申请
    handleDelete() {
      const { id } = this.multipleSelection[0]
      this.$router.push({
        path: '/PersonalOffice/enterpriseInfo',
        query: { id }
      })
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    }
    /* 按钮 end */
  }
}
</script>

